/// @FUNCTION
/// @NAME XfHashGet
/// @DESC 
/// @ACCESS Server
/// @MP False
/// @PARAM Array The hash table
/// @PARAM String The name of the key in the hash you want to retrieve
/// @PARAM Variant The default value that you expect out of the method if the key is not found
/// @RETURN The value contained in the hash or the default
/// @REQUIRE Nothing
/// @REMARK
/// @EXAMPLE [_instances, "Reinforcements", [],THIS_FILE] call FFUNC(hashGet);

//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "arrays\fn_hashGet.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>
#ifndef __DUMP_INDENT__
#define __DUMP_INDENT__	"    "
#endif

private ["_hash_array","_hash_key","_index","_hash_values","_result","_default"];
TRACE_1("Start",_this);
TMFPROFILERSTART;
DEFAULT_PARAM(_hash_array,0,[]);
DEFAULT_PARAM(_hash_key,1,"");
DEFAULT_PARAM(_default,2,nil);
_result = VALORNIL(_default);
If (IS_ARRAY(_hash_array) && !isnil "_hash_key") then {
	if (!IS_STRING(_hash_key)) then {
		_hash_key = str(_hash_key);
	};
	if (IS_STRING(_hash_key)) then {
		_hash_key = toUpper _hash_key;
	};
	if (_hash_key != "") then {
		// For safety and easyness, we Upper all keys internally
		// Normally "== 2" is enough since a hash only needs a key indexer and a value array, but in the future we might extend it to more keys for multi dimensional storage
		//  So they 1st dimension keys array is always the first and the values container is always the last one
		if (count _hash_array >= 2) then {
			_index = (_hash_array select 0) find _hash_key;
			if (_index >= 0) then {
				_hash_values = _hash_array select (count _hash_array - 1);
				if (_index < count _hash_values) then {
					_result = _hash_values select _index;
				};
			};
		} else {
			ERROR_1("Invalid Hash Array",_this);
		};
	} else {
		ERROR_1("Invalid Key",_key);
	};
} else {
	ERROR_1("Invalid Parameters",_this);
};
TRACE_1("End",VALORNIL(_result));
TMFPROFILERSTOP;
_result;